package web;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import vo.User;
import vo.UserGroup;
import business.UserGroupService;
import business.UserService;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.Preparable;

@SuppressWarnings("unchecked")
public class UserGroupAction implements Preparable {
	private UserGroupService service;
	private UserService userService;
	private List<UserGroup> userGroups;
	private String name;
	private String role;
	private ArrayList<String> delIds = new ArrayList<String>();

	private User curUser;

	public String listUserGroup() {
		this.userGroups = service.getAllUserGroups();
		return Action.SUCCESS;
	}

	public String addUserGroup() {
		service.addUserGroup("hello xxx");
		return listUserGroup();
	}

	public String deleteUserGroup() {
		System.out.println("delete group:");
		for (int i = 0; i < this.delIds.size(); i++) {
			System.out.println("deleted ids：" + delIds.get(i));
			long id = Long.parseLong(delIds.get(i));
			service.delete(id);

		}
		return "userGroupDeleted";
	}

	public String saveUserGroup() {
		System.out.println("form save group:" + name + "," + role);
		UserGroup g = new UserGroup();
		g.setName(name);
		g.setRole(role);
		service.addUserGroup(g);
		return "saveUserGroup";
	}

	public UserGroupService getService() {
		return service;
	}

	public void prepare() throws Exception {
		ActionContext ctx = ActionContext.getContext();
		Map session = ctx.getSession();
		String username = (String) session.get("username");
		curUser = userService.getByLoginName(username);
		System.out.println("curUser.getLoginName():" + curUser.getLoginName());
	}

	public void setUserGroupService(UserGroupService service) {
		this.service = service;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getRole() {
		return role;
	}

	public void setRole(String role) {
		this.role = role;
	}

	public ArrayList<String> getDelIds() {
		return delIds;
	}

	public void setDelIds(ArrayList<String> delIds) {
		this.delIds = delIds;
	}

	public void setService(UserGroupService service) {
		this.service = service;
	}

	public List<UserGroup> getUserGroups() {
		return userGroups;
	}

	public void setUserGroups(List<UserGroup> userGroups) {
		this.userGroups = userGroups;
	}

	public User getCurUser() {
		return curUser;
	}

	public void setCurUser(User curUser) {
		this.curUser = curUser;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

}